﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Task.Common.Interface;
using Common.Service;
using System.Collections;
using Common.BasePages;
using Common.Configuration;
using OC.Common.Entity;


namespace Task
{
    public partial class MyTask : ProcessBasePage
    {
        ITask task = ServiceContainer.GetService<ITask>();
        ITaskStatus taskStatus = ServiceContainer.GetService<ITaskStatus>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Index = "0";
                LoadpageDate(0);
                LoadFinPageData(0);
                
            }
        }
        public string Index { get; set; }
        /// <summary>
        /// 待办总数
        /// </summary>
        public string InboxCount { get; set; }
        /// <summary>
        /// 已办总数
        /// </summary>
        public string FinInboxCount { get; set; }

        #region 待办
        private void LoadpageDate(int index)
        {
            int pageSize = 10;
            int count = 0;
            IList list = task.GetList(GetWhereCondition(),0,10,out count);
            PagedDataSource page = new PagedDataSource();

            int totalCount = list.Count;
            InboxCount = totalCount.ToString();
            //DataView dv = dt.DefaultView;
            page.DataSource = list;
            page.AllowPaging = true;
            page.PageSize = pageSize;
            page.CurrentPageIndex = index;

            rpMyTask.DataSource = page;
            rpMyTask.DataBind();

            pagination.RecordCount = totalCount;
            pagination.PageSize = page.PageSize;
            pagination.CurrentPageIndex = page.CurrentPageIndex + 1;
            pagination.Visible = true;
            pagination.AlwaysShow = true;
            pagination.CurrentPageButtonClass = "btnPageNavCurrentInput";
            pagination.DataBind();
        }

        private string GetWhereCondition()
        {
            string returnString=String.Empty;
            //username 为当前任务的处理人

            string userName = DomainName + "/" + ((OC.Common.Entity.UserEntity)UserInfo).AD_ACCOUNT;
            returnString += "username!"+userName.ToUpper()+";processLabel!" + processName.Value + ";bussinessNum!" + bussinessID.Value + ";taskuser!" + applicantUser.Value+ ";";
            if (ddlProcessCatalog.SelectedValue != "0")
            {
                returnString += "processname!" + ddlProcessCatalog.SelectedItem.Text + ";";
            }
            else
            {
                returnString += "processname!;";
            }
            if (!String.IsNullOrEmpty(startDate.Value) && !String.IsNullOrEmpty(endDate.Value))
            {
                returnString += "timeSql!and (v.starttime between to_date('" + startDate.Value + "','yyyy-MM-dd') and to_date('" + endDate.Value + "','yyyy-MM-dd'));";
            }
            //任务状态
            if (ddlTaskStatus.SelectedValue != "0")
            {
                if (ddlTaskStatus.SelectedValue == "1")//新任务 --新送达的，没有浏览的
                {
                    returnString += "taskStatus!and v.URGENTDUETIME is null and (select count(*) from t_taskstatus ts where ts.taskid=v.taskid)=0;";
                }
                if (ddlTaskStatus.SelectedValue == "2")//待处理--已打开浏览过，但未处理，并且没有设定截止日期
                {
                    returnString += "taskStatus!and v.URGENTDUETIME is null and (select count(*) from t_taskstatus ts where ts.taskid=v.taskid)=1;";
                }
                if (ddlTaskStatus.SelectedValue == "3")//已超期
                {
                    returnString += "taskStatus!and v.substatus=2";
                }
                if (ddlTaskStatus.SelectedValue == "4")//即将到期 
                {
                    returnString += "taskStatus!and v.URGENTDUETIME is not null and (v.URGENTDUETIME-sysdate)<=1 and (v.URGENTDUETIME-sysdate)>0";
                }
            }
            
            //所属流程
            return returnString;
        }

        #endregion

        #region 已办
        private void LoadFinPageData(int index)
        {
            int pageSize = 10;
            int count = 0;
            IList list = task.GetFinishedList(GetFinWhereCondition(), 0, 10, out count);
            PagedDataSource page = new PagedDataSource();

            int totalCount = list.Count;
            FinInboxCount = totalCount.ToString();
            //DataView dv = dt.DefaultView;
            page.DataSource = list;
            page.AllowPaging = true;
            page.PageSize = pageSize;
            page.CurrentPageIndex = index;

            rpCompleteTask.DataSource = page;
            rpCompleteTask.DataBind();

            pagination.RecordCount = totalCount;
            pagination.PageSize = page.PageSize;
            pagination.CurrentPageIndex = page.CurrentPageIndex + 1;
            pagination.Visible = true;
            pagination.AlwaysShow = true;
            pagination.CurrentPageButtonClass = "btnPageNavCurrentInput";
            pagination.DataBind();
        }
        private string GetFinWhereCondition()
        {
            string returnString = String.Empty;
            string userName = DomainName + "/" + ((UserEntity)UserInfo).AD_ACCOUNT;
            returnString += "username!" + userName.ToUpper() + ";processLabel!" + processNameCom.Value + ";bussinessNum!" + bussinessIDCom.Value + ";taskuser!" + applicantUserCom.Value + ";";
            if (ddlFinProcess.SelectedValue != "0")
            {
                returnString += "processname!" + ddlFinProcess.SelectedItem.Text + ";";
            }
            else
            {
                returnString += "processname!;";
            }
            if (!String.IsNullOrEmpty(startDateCom.Value) && !String.IsNullOrEmpty(endDateCom.Value))
            {
                returnString += "timeSql!and (v.endtime between to_date('" + startDateCom.Value + "','yyyy-MM-dd') and to_date('" + endDateCom.Value + "','yyyy-MM-dd'));";
            }
            //任务状态
            return returnString;
        }
        #endregion

        #region 显示任务的状态
        /// <summary>
        /// 在待办中 显示 任务的状态信息
        /// </summary>
        /// <param name="urgentTime">截止时间</param>
        /// <param name="taskID">taskid-用于查询新任务/待处理</param>
        /// <returns></returns>
        protected string GetTaskStatus(string urgentTime,string taskID)
        {
            string returnString = String.Empty;
            if (String.IsNullOrEmpty(urgentTime))//如果没有设定 截止时间,只有两个状态-新任务，待处理
            {
                //根据任务的TaskID,查询T_TASKSTATUS表，有记录则状态为 待处理 否则为 新任务
                int count = 0;
                IList listTaskStatus = taskStatus.GetList(taskID.Trim(), 0, 10,out count);
                if (listTaskStatus.Count > 0)
                {
                    returnString = "待处理";
                }
                else
                {
                    returnString = "新任务";
                }
            }
            else
            {
                DateTime dueTime = DateTime.Parse(urgentTime);//截止时间
                DateTime nowTime = DateTime.Now;//当前时间
                TimeSpan ts = dueTime - nowTime;
                if (ts.Seconds < 0)//超期
                {
                    returnString += "超期";
                    if (Math.Abs(ts.Days) >= 1)
                    {
                        returnString += Math.Abs(ts.Days) + "天" + Math.Abs(ts.Hours) + "小时";
                    }
                    else
                    {
                        returnString += Math.Abs(ts.Hours) + "小时";
                    }
                }
                else
                {
                    if (ts.Days >= 1)
                    {
                        returnString += ts.Days + "天" + ts.Hours + "小时";
                    }
                    else
                    {
                        returnString += ts.Hours + "小时";
                    }
                    returnString += "到期";
                }
            }
            return returnString;
        }
        #endregion

        /// <summary>
        /// 分页选择
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void pagination_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            LoadpageDate(e.NewPageIndex - 1);
        }

        /// <summary>
        /// 分页选择
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void pagination2_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            //LoadPageData(e.NewPageIndex - 1);
            LoadFinPageData(e.NewPageIndex - 1);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadpageDate(0);
            Index = "0";
        }

        protected void btnSearchCom_Click(object sender, EventArgs e)
        {
            LoadFinPageData(0);
            Index = "1";
        }
    }
}